package com.simple.admin.controller.system;

import com.simple.framework.constant.HttpStatus;
import com.simple.framework.core.controller.BaseController;
import com.simple.framework.core.domain.R;
import com.simple.system.annotation.SystemLog;
import com.simple.system.domain.dto.AddSysDeptDto;
import com.simple.system.domain.vo.SysDeptDetailsVo;
import com.simple.system.domain.vo.SysListDeptVo;
import com.simple.system.enums.BusinessType;
import com.simple.system.service.SysDeptService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description 部门管理
 * @Author xiaoke
 * @Date 2024/8/10 18:00
 * @Version 1.0
 */
@RestController
@RequestMapping("sys_dept")
public class SysDeptController extends BaseController {

    private final SysDeptService sysDeptService;

    public SysDeptController(SysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

    /**
     * 获取部门树
     * @return List<SysDept>
     */
    @GetMapping
    public R<List<SysListDeptVo>> deptTree(){
        return success(sysDeptService.listDeptTree());
    }

    /**
     * 获取部门详情
     * @param id 部门Id
     * @return SysDeptDetailsVo
     */
    @GetMapping("{id}")
    public R<SysDeptDetailsVo> get(@PathVariable("id") Long id) {
        return success(sysDeptService.get(id));
    }

    /**
     * 新增部门
     * @param dto 新增参数
     * @return 部门Id
     */
    @SystemLog(title = "新增部门",businessType = BusinessType.INSERT)
    @PostMapping
    public R<Long> create(@RequestBody @Validated AddSysDeptDto dto) {
        return success(HttpStatus.CREATED,sysDeptService.create(dto));
    }

    /**
     * 更新部门
     * @param id 部门Id
     * @param dto 更新参数
     */
    @SystemLog(title = "修改部门",businessType = BusinessType.UPDATE)
    @PutMapping("{id}")
    public R<Void> update(@PathVariable("id") Long id, @RequestBody @Validated AddSysDeptDto dto) {
        sysDeptService.update(id,dto);
        return success();
    }

    /**
     * 删除部门
     * @param id 部门Id
     */
    @SystemLog(title = "删除部门",businessType = BusinessType.DELETE)
    @DeleteMapping("{id}")
    public R<Void> delete(@PathVariable("id") Long id) {
        sysDeptService.delete(id);
        return success();
    }

}
